/**
 * @param {Number} val
 * @param {ListNode} next
 */
function ListNode(val, next) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
}

/**
 * @param {Array} list
 * @returns {LinkedList}
 */
function LinkedList(list) {
    this.head = null;
    this.tail = null;
    for (const num of list) {
        if (!this.head) {
            this.head = this.tail = new ListNode(num);
        } else {
            this.tail.next = new ListNode(num);
            this.tail = this.tail.next;
        }
    }
}
